<import type="js" file="common/sortable/jqueryui189" />
<import type="js" file="common/sortable/jqueryuisortablemin" />
<div>
    <div style="float:left"><input type="file" id="upload[id]" name="upload[id]" />
    <input type="hidden" id="[id]" name="[id]" value="[value]" /></div> <div id="msg_paixu" style="float:left;padding-left:20px;padding-top:10px;color:#555;">拖动图片可排序</div><div class="c"></div>
    <div id="div[id]" class="piclist" style=" margin:10px 0px;">
    </div>
    <div class="c"></div>
</div>
<script type="text/javascript">
	var value = "[value]";
	if(value == "")
	{
		$("#div[id]").hide();
	}
	$('#upload[id]').uploadify({
		'swf': '__PUBLIC__/common/uploadify/uploadify.swf")',
		'uploader': "{:U('Fwadmin/Login/uploadMorePic')}?path=[path]&thumbw=[thumbw]&thumbh=[thumbh]&sessionid={:session_id()}",
		'cancelImg': '__PUBLIC__/common/uploadify/uploadify-cancel.png',
		'buttonText':'选择多张图片',
		'auto'     : true,
		'multi'    : true,  //允许上传多图
		'height': 24,
		'width': 120,
		'fileTypeDesc': '图片文件',
		'fileTypeExts':'*.jpg;*.gif;*.png;*.bmp;',
		'fileSizeLimit':'2024KB',
		'removeTimeout':0,
		'onUploadError': function (file, errorCode, errorMsg, errorString, swfuploadifyQueue) {
			if(errorCode !== "-280" && errorMsg !== "File Upload Cancelled.")
			{
				alert(errorMsg);
			}
		},
		'onCancel':function(event,queueID,file,data){
		   //  alert("取消成功！");
		},
		'onUploadComplete':function (file) { //每个文件上传完成
			
		},
		'onUploadSuccess': function (file, data, response) {
		　//　　alert(file+'-'+data+'--'+response);
			if (response == true) {
				var json = eval('('+data+')');
				if(json.SaveName != undefined)
				{
				   $("#[id]").val($("#[id]").val()+json.SaveName+',');
				   showMorePic($("#[id]").val());
				}
			}
			else {
				alert("上传出错！");
			}
		}
	});
	
	//uploadify上传多文件删除文件
	function delfile2(filename,i) {
		if(filename!=='')
		{
		   $.ajax({
			  url:"{:U('Fwadmin/Index/delPic')}?filename="+filename,
			  success:function(val)
			  {
				  var old_val = $("#[id]").val();
				  var new_val = old_val.replace(filename+',','');
				  $("#[id]").val(new_val);
				  $("#showpic"+i).hide();
			  }
		  });
		  var filename2 = filename.replace('[path]','[path]/thumb');
		  $.ajax({
			  url:"{:U('Fwadmin/Index/delPic')}?filename="+filename2,
			  success:function(val)
			  {
			  }
		  })
		}
	}
	function showMorePic(str)　{
			var html = '';
			if(str !== '')
			{
				str = str.substring(0,str.length-1);
				//str = str.TrimEnd(',');
				var pics = str.split(',');
				for(var i=0;i<pics.length;i++)
				{
					if(pics[i] !== '')
					{
						html += '<dl id="showpic'+i+'" val="'+pics[i]+'"><dt><a href="__ROOT__'+pics[i]+'" target="_blank"><img src="__ROOT__'+pics[i]+'" width="[width]" alt="点击查看原图"  /></a></dt><dd><a href="#" onclick="delfile2(\''+pics[i]+'\','+i+')">删除</a></dd></dl>';
					}
				}
				$("#div[id]").html(html);
				$("#div[id]").show();
			} else {
				$("#div[id]").html(html);
				$("#div[id]").hide();
			}	
	} 
	
	//启动排序
	function paixu_enable() {
		$(".piclist" ).sortable({
				revert:false,
				items:'dl',
				 update:function(event, ui){paixu();}
			});
	}
	//图片排序
	function paixu() {
		$("#msg_paixu").html('<font color=red>排序正在保存中...</font>');
		var child = $(".piclist").children();
		var success_num = 0;
		var str='';
		for(i=0;i<child.size();i++){
			var val = child.eq(i).attr("val");
			var orderid = i+1;
			str += val+',';
		}
		 $("#[id]").val(str);
		 $("#msg_paixu").html('<font color=green>排序保存成功</font>');
	}
	if($("#[id]").val() != '')
	{
		showMorePic($("#[id]").val());
	}
	paixu_enable();
</script>